using System;

namespace Cfg.ValidationAttributes
{
    public class MinLengthAttribute : CfgFinalizedStringValueValidatorBaseAttribute
    {
        public int MinLength { get; private set; }
        public MinLengthAttribute(int minLength, params string[] contexts)
        {
            Contexts = contexts;
            MinLength = minLength;
        }

        protected override void Validate(string finalizedStringValue)
        {
            if (finalizedStringValue == null)
                finalizedStringValue = string.Empty;

            if (finalizedStringValue.Length < MinLength)
            {
                var message = string.Format("Minimum length is set to({0}), but value '{1}' has length {2}",
                    MinLength,
                    finalizedStringValue,
                    finalizedStringValue.Length);

                throw new Exception(message);
            }
        }
    }
}